package com.ssy.lingxi.logistics.controller.mobile;

import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.model.vo.PageVO;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.logistics.api.dto.request.CommonIdRequest;
import com.ssy.lingxi.logistics.model.request.ReceiverAddressAddRequest;
import com.ssy.lingxi.logistics.model.request.ReceiverAddressUpdateRequest;
import com.ssy.lingxi.logistics.model.response.ReceiverAddressResponse;
import com.ssy.lingxi.logistics.model.response.mobile.MobileReceiverAddressListResponse;
import com.ssy.lingxi.logistics.service.IMobileReceiverAddressService;
import com.ssy.lingxi.logistics.service.IReceiverAddressService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @menu App - 收货地址管理
 * @author yzc
 * @version 2.0.0
 * @date 2020/12/22
 */
@RestController
@RequestMapping("/logistics/mobile/receiverAddress")
public class MobileReceiverAddressController extends BaseController {

    @Resource
    private IReceiverAddressService receiverAddressService;
    @Resource
    private IMobileReceiverAddressService mobileReceiverAddressService;

    /**
     * 收货地址分页列表
     * @param request 接口参数
     * @return 返回结果
     */
    @GetMapping("/page")
    public Wrapper<PageData<MobileReceiverAddressListResponse>> pageReceiverAddress(PageVO request) {
        UserLoginCacheDTO sysUser = getSysUser();
        return mobileReceiverAddressService.pageReceiverAddress(request, sysUser);
    }

    /**
     * 收货地址列表(默认地址首位)
     * @return 返回结果
     */
    @GetMapping("/list/default")
    public Wrapper<List<MobileReceiverAddressListResponse>> listReceiverAddressDefault() {
        UserLoginCacheDTO sysUser = getSysUser();
        return mobileReceiverAddressService.listReceiverAddressDefault(sysUser);
    }

    /**
     * 收货地址查询
     *
     * @param id 主键id
     * @return 返回结果
     */
    @GetMapping(value = "/get")
    public Wrapper<ReceiverAddressResponse> getReceiverAddress(@RequestParam(name = "id") Long id) {
        UserLoginCacheDTO sysUser = getSysUser();
        return receiverAddressService.getReceiverAddress(id);
    }

    /**
     * 收货地址添加
     *
     * @param request 接口参数
     * @return 返回结果
     */
    @PostMapping(value = "/add")
    public Wrapper<ReceiverAddressResponse> addReceiverAddress(@RequestBody @Validated ReceiverAddressAddRequest request) {
        UserLoginCacheDTO sysUser = getSysUser();
        return receiverAddressService.addReceiverAddress(request, sysUser);
    }

    /**
     * 收货地址修改
     * @param request 接口参数
     * @return 返回结果
     */
    @PostMapping(value = "/update")
    public Wrapper<Void> updateReceiverAddress(@RequestBody @Validated ReceiverAddressUpdateRequest request) {
        UserLoginCacheDTO sysUser = getSysUser();
        return receiverAddressService.updateReceiverAddress(request, sysUser);
    }

    /**
     * 收货地址删除
     * @param request 接口参数
     * @return 返回结果
     */
    @PostMapping(value = "/delete")
    public Wrapper<Void> deleteReceiverAddress(@RequestBody CommonIdRequest request) {
        UserLoginCacheDTO sysUser = getSysUser();
        return receiverAddressService.deleteReceiverAddress(request, sysUser);
    }
}
